package araujoCoronelVillamayor.plantasVsZombies.uiWicket;

import araujoCoronelVillamayor.plantasVsZombies.appModelsArena.AtaqueAppModel
import araujoCoronelVillamayor.plantasVsZombies.uiWicket.layouts.LayoutPrincipal
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.model.CompoundPropertyModel
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XLink
import org.uqbar.wicket.xtend.XListView
import araujoCoronelVillamayor.plantasVsZombies.domain.Fila
import araujoCoronelVillamayor.plantasVsZombies.domain.Zombie
import org.apache.wicket.markup.html.form.DropDownChoice
import org.uqbar.commons.model.UserException
import org.apache.wicket.markup.html.panel.FeedbackPanel
import araujoCoronelVillamayor.plantasVsZombies.appModelsArena.SesionAppModel
import org.apache.wicket.markup.html.form.Form
import org.uqbar.wicket.xtend.XButton
import org.uqbar.wicket.xtend.XAttributeModifier
import org.apache.wicket.model.IModel
import java.util.List

class AtaquePage extends LayoutPrincipal {
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	
	private final JardinPage mainPage
	private final Fila filaAAtacar
	private final AtaqueAppModel ataqueAppModel
	
	new(Fila filaParametro, SesionAppModel sesionAppModelParametro, JardinPage mainPage) {
		super();
		
		this.mainPage = mainPage
		defaultModel = new CompoundPropertyModel(sesionAppModelParametro.getAtaqueAppModel)     
        
        this.filaAAtacar = filaParametro
        this.ataqueAppModel = sesionAppModelParametro.getAtaqueAppModel
        
        this.addChild(new Label("tituloAtaque", "Atacar fila " + (sesionAppModelParametro.getJardin.getFilas.indexOf(filaParametro)+1).toString))
        
        val atacarForm = new Form<AtaqueAppModel>("nuevoAtaqueForm", ataqueAppModel.asCompoundModel)
		this.agregarCamposSeleccion(atacarForm)
		this.agregarAcciones(atacarForm)
		this.addChild(atacarForm)
														 
		this.addChild(new FeedbackPanel("feedbackPanel"))
		
		val panelEstadoJardin = new EstadoJardinPanel("panelEstadoJardin", sesionAppModelParametro);
		this.addChild(panelEstadoJardin);

		this.agregarGrillaAtaqueLog()
    }
    
    def volver() {
		mainPage.refreshFilas()
		responsePage = mainPage
	}
    
    def agregarCamposSeleccion(Form<AtaqueAppModel> parent) {
    	parent.addChild(new DropDownChoice<Zombie>("zombieSeleccionado") => [
			choices = loadableModel([| ataqueAppModel.getZombies ])
			choiceRenderer = choiceRenderer([Zombie z| z.nombre ])
		])
    }
    
    def void agregarAcciones(Form<AtaqueAppModel> parent) {
    	parent.addChild(new XButton("botonAtacar") => [
			onClick = [| atacar() ]
        ])
        parent.addChild(new XButton("volverJardin") => [
			onClick = [| volver() ]
        ])

    }
    
    def atacar(){
		try {
			ataqueAppModel.getSesionAppModel.filaSeleccionada = filaAAtacar
			//ataqueAppModel.getSesionAppModel.zombieSeleccionado = ataqueAppModel.getZombies.get(0)
			//ataqueAppModel.setZombieSeleccionado = ataqueAppModel.getZombies.get(0)
			ataqueAppModel.atacar(filaAAtacar, ataqueAppModel.getZombieSeleccionado) //ataqueAppModel.getZombies.get(0))
			ataqueAppModel.refresh()
		} catch (UserException e) {
			error(e.getMessage())
		}
    }
    
    def void agregarGrillaAtaqueLog(){
		val listViewAtaque = new XListView("resultadosAtaqueLog")		
		listViewAtaque.populateItem = [ item |
			item.model = item.modelObject.asCompoundModel
			item.addChild(new Label("mensaje"))
			item.addChild(new Label("atacanteStats"))
			item.addChild(new Label("defensorStats"))			
		]
		this.addChild(listViewAtaque);
    }	
   
 	def getAtaqueAppModel() {
		this.defaultModelObject as AtaqueAppModel
	}
}
